package cn.szzsi._webpc;

import cn.szzsi._web.BaseWebController;
import cn.szzsi.common.kit.RegexUtils;
import cn.szzsi.common.model.ProductCategory;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.common.model.zshop.CompanyInfo;
import cn.szzsi.common.model.zshop.CompanyInfoService;

import cn.szzsi.common.model.zshop.Member;
import cn.szzsi.core.kit.CommonUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import javax.print.attribute.standard.Media;
import java.util.Date;
import java.util.List;

public class RecruitmentController extends BaseWebPcController<Member> {
    public void index(){
        if(getLoginMember()!=null){
            setAttr("loginMember",getLoginMember());
            render("/template/pc1/html/recruitment.html");
        }else{
            render("/index.html");
        }

    }

    @Clear
    @Before(Tx.class)
    public void save(){
        String realName=getPara("name");//姓名
        String phone=getPara("phone");//手机号
        String weChat=getPara("weChat");//微信号
        String qq=getPara("qq");//
        String product=getPara("product");
        if(getLoginMember()!=null){
            phone=getLoginMember().getPhone();
        }
        if(StrKit.isBlank(weChat)){
            renderJson(Ret.fail("info","微信号不允许为空!"));
            return;
        }
        if(StrKit.isBlank(product)){
            renderJson(Ret.fail("info","产品不允许为空!"));
            return;
        }
        CompanyInfo companyInfo=CompanyInfo.dao.getByPhone(phone);
        if(companyInfo!=null){
            if("1".equals(companyInfo.getStatus())){
                renderJson(Ret.fail("info","该商正在审核中,请耐心等待审核结果!"));
                return;
            }
            if("3".equals(companyInfo.getStatus())){
                renderJson(Ret.fail("info","该商户审核失败,请重新提交审核!"));
                return;
            }

            if("2".equals(companyInfo.getStatus())){
                renderJson(Ret.fail("info","该商户已入住!"));
                return;
            }
        }else{
            //1.保存商户信息
            Member member=new Member().use(CommonUtil.ZSHOP);
            member.setId(StrKit.getRandomUUID());
            member.setUsername(phone);
            member.setPhone(phone);
            member.set("shopId", "0");
            member.set("createDate", new Date());
            member.set("roleId", "super");
            member.set("b_shoprank", "super");
            member.save();
            //2.保存商户审核状态
            CompanyInfoService.cis.saveInfo(phone,"",realName,"","1",member.getId(),weChat,qq,product);

            //3.保存siteconfig
            SiteConfig sc=new SiteConfig();
            sc.setUid(member.getId());
            sc.setPhone(phone);
            sc.setShopAlias(phone);
            sc.setCreateDate(new Date());
            sc.save();
            renderJson(Ret.ok());
        }

    }

    @Clear
    public void update(){
        String realName=getPara("name");//姓名
        String phone=getPara("phone");//手机号
        String weChat=getPara("weChat");//微信号
        String qq=getPara("qq");
        String product=getPara("product");

        if(StrKit.notBlank(phone)&&!RegexUtils.checkMobile(phone)){
            renderJson(Ret.fail("info","请输入正确手机号!"));
            return;
        }

        if(StrKit.isBlank(weChat)){
            renderJson(Ret.fail("info","微信号不允许为空!"));
            return;
        }
        Member member=Member.dao.getByPhone(phone);
        if(member==null){
            renderJson(Ret.fail("info","手机号不存在!"));
            return;
        }
        member.update();
        CompanyInfo companyInfo=CompanyInfo.dao.getByPhone(phone);
        companyInfo.setStatus("1");
        companyInfo.setPersonInCharge(realName);
        companyInfo.setWeChat(weChat);
        companyInfo.setQq(qq);
        companyInfo.setProduct(product);
        companyInfo.update();
        renderJson(Ret.ok("info","提交成功!"));
    }

}
